import { _decorator, Component, director } from 'cc';
import { Cmd, Stype } from '../Config/GameCfg';
import { decodeGameSceneTzReq, decodeGameSceneTzRes, decodeStartAnswerTzEndGetRewardReq, decodeStartAnswerTzEndGetRewardRes, decodeStartAnswerTzEndNotice, decodeStartAnswerTzErrNotice, decodeStartAnswerTzFuHuoReq, decodeStartAnswerTzFuHuoRes, decodeStartAnswerTzNextNotice, decodeStartAnswerTzReq, decodeStartAnswerTzRes, decodeStartAnswerTzTimeOutReq, decodeStartAnswerTzTimeOutRes, encodeGameSceneTzReq, encodeStartAnswerTzEndGetRewardReq, encodeStartAnswerTzFuHuoReq, encodeStartAnswerTzReq, encodeStartAnswerTzTimeOutReq, GameSceneTzReq, StartAnswerTzEndGetRewardReq, StartAnswerTzFuHuoReq, StartAnswerTzReq, StartAnswerTzTimeOutReq } from '../../AssetPackage/Props/Game/DT_TZ';
import { EventMsgKes } from '../Config/ResConfig';
import { GameData } from './GameData';
import { decodeThemeMatchGameReq, decodeThemeMatchGameRes, encodeThemeMatchGameReq, ThemeMatchGameRecordReq, ThemeMatchGameReq } from '../../AssetPackage/Props/Game/DT_ZT';
import { encodeMsg, Msg } from '../../AssetPackage/Props/DT';
const { ccclass, property } = _decorator;

// 解析游戏的数据
@ccclass('MsgDataGame')
export class MsgDataGame extends Component {
    private static _inst: MsgDataGame;
    public static get inst(): MsgDataGame {
        if (this._inst == null) {
            this._inst = new MsgDataGame();
        }

        return this._inst;
    }

    // MsgData.inst.sendDate(Stype.Game,ctype,buf)
    private sendDate(ctype:number, buf:Uint8Array){
        let msg:Msg = {
            stype:Stype.Game,
            ctype: ctype,
            buffer: buf,
        };
        //编码协议数据
        let buf1:Uint8Array = encodeMsg(msg);
        //去掉数据编码产生的0
        let str = buf1.toString();
        let arr = str.split(',');
        let arr2 = []
        arr.forEach(element => {
            arr2.push(parseInt(element));
        });
        const uint8Array1 = new Uint8Array(arr2);
        console.error('=========sendDate Login========',ctype);
        let arrbuffer = uint8Array1.buffer;
        // NetMgr.inst.send(arrbuffer);
    }

    //每日挑战请求游戏场景
    public GameSceneTzReq(){
        let req: GameSceneTzReq = {
            userId: GameData.inst.userInfo.userId,
        }
        let uint8Array:Uint8Array  = encodeGameSceneTzReq(req);
        console.log("======GameSceneTzReq 编码======",uint8Array);
        console.log('======GameSceneTzReq 解码======',decodeGameSceneTzReq(uint8Array));
        this.sendDate(Cmd.Game.GameSceneTzReq,uint8Array);
    }

    //每日挑战请求答题
    public StartAnswerTzReq(tid: number, cid: number){
        let req: StartAnswerTzReq = {
            userId: GameData.inst.userInfo.userId,
            tid: tid,
            cid: cid
        }
        let uint8Array:Uint8Array = encodeStartAnswerTzReq(req);
        console.log("======StartAnswerTzReq 编码======",uint8Array);
        console.log('======StartAnswerTzReq 解码======',decodeStartAnswerTzReq(uint8Array));
        this.sendDate(Cmd.Game.StartAnswerTzReq,uint8Array);
    }

    //每日挑战请求答题超时
    public StartAnswerTzTimeOutReq(tid: number){
        let req: StartAnswerTzTimeOutReq = {
            userId: GameData.inst.userInfo.userId,
            tid: tid,
        }
        let uint8Array:Uint8Array  = encodeStartAnswerTzTimeOutReq(req);
        console.log("======StartAnswerTzTimeOutReq 编码======",uint8Array);
        console.log('======StartAnswerTzTimeOutReq 解码======',decodeStartAnswerTzTimeOutReq(uint8Array));
        this.sendDate(Cmd.Game.StartAnswerTzTimeOutReq,uint8Array);
    }

    //每日挑战--结算请求领取奖励
    public StartAnswerTzEndGetRewardReq(bei:number){
        let req:StartAnswerTzEndGetRewardReq = {
            userId: GameData.inst.userInfo.userId,
            beiShu: bei
        }
        let uint8Array:Uint8Array  = encodeStartAnswerTzEndGetRewardReq(req);
        console.log("======StartAnswerTzEndGetRewardReq 编码======",uint8Array);
        console.log('======StartAnswerTzEndGetRewardReq 解码======',decodeStartAnswerTzEndGetRewardReq(uint8Array));
        this.sendDate(Cmd.Game.StartAnswerTzEndGetRewardReq,uint8Array);
    }

    //每日挑战--复活
    public StartAnswerTzFuHuoReq(){
        let req: StartAnswerTzFuHuoReq = {
            userId: GameData.inst.userInfo.userId,
            tid: 0
        }
        let uint8Array:Uint8Array = encodeStartAnswerTzFuHuoReq(req);
        console.log("======StartAnswerTzFuHuoReq 编码======",uint8Array);
        console.log('======StartAnswerTzFuHuoReq 解码======',decodeStartAnswerTzFuHuoReq(uint8Array));
        this.sendDate(Cmd.Game.StartAnswerTzFuHuoReq,uint8Array);
    }


    

    //请求答题回顾
    public ThemeMatchGameReWardReq(){
        let d:ThemeMatchGameRecordReq = {
            userId: '0'
        }
        // if(this.isTest){
        //     TestData.inst.onThemeMatchGameRecordReqTest();
        // }
    }

    onMsg(ctype: number, msg:Uint8Array){
        switch (ctype) {
           
            case Cmd.Game.GameSceneTzRes: //每日挑战请求游戏场景
                this.onGameSceneTzRes(msg);
                break;    
            case Cmd.Game.StartAnswerTzRes://每日挑战请求开始答题
                this.onStartAnswerTzRes(msg);
                break;
            case Cmd.Game.StartAnswerTzErrNotice: //每日挑战用户答题结果通知
                this.onStartAnswerTzErrNotice(msg);
                break;        
            case Cmd.Game.StartAnswerTzTimeOutRes: //每日答题超时
                this.onStartAnswerTzTimeOutRes(msg);
                break; 
            case Cmd.Game.StartAnswerTzNextNotice://每日答题--通知下一题
                this.onStartAnswerTzNextNotice(msg);
                break;       
            case Cmd.Game.StartAnswerTzEndNotice: //每日答题--结算通知
                this.onStartAnswerTzEndNotice(msg);
                break;    
            case Cmd.Game.StartAnswerTzEndGetRewardRes://每日答题--结算领取奖励
                this.onStartAnswerTzEndGetRewardRes(msg);
                break;    
            case Cmd.Game.StartAnswerTzFuHuoRes: //每日挑战--复活
                this.onStartAnswerTzFuHuoRes(msg);
                break;
        
            case Cmd.Game.ThemeMatchGameRes:    //主题赛--- 场景
                this.onThemeMatchGameRes(msg);
                break;

            default:
                break;
        }
    }

    //====================================每日挑战 start===============================================
    // 每日挑战请求游戏场景
    private onGameSceneTzRes(buffer: Uint8Array): void {
        let data = decodeGameSceneTzRes(buffer);
        console.log('========onGameSceneTzRes=============', data);
        director.emit(EventMsgKes.msgGameSceneTzRes, data);
    }

    // 每日挑战请求开始答题返回
    private onStartAnswerTzRes(buffer: Uint8Array): void {
        let data = decodeStartAnswerTzRes(buffer);
        console.log('========onStartAnswerTzRes=============', data);
        director.emit(EventMsgKes.msgStartAnswerTzRes, data);
    }

    // 每日挑战用户答题结果通知
    private onStartAnswerTzErrNotice(buffer: Uint8Array): void {
        let data = decodeStartAnswerTzErrNotice(buffer);
        console.log('========onStartAnswerTzErrNotice=============', data);
        director.emit(EventMsgKes.msgStartAnswerTzErrNotice, data);
    }

    // 每日答题超时
    private onStartAnswerTzTimeOutRes(buffer: Uint8Array): void {
        let data = decodeStartAnswerTzTimeOutRes(buffer);
        console.log('========onStartAnswerTzTimeOutRes=============', data);
        director.emit(EventMsgKes.msgStartAnswerTzTimeOutRes, data);
    }

    //每日答题--通知下一题
    private onStartAnswerTzNextNotice(buffer: Uint8Array): void {
        let data = decodeStartAnswerTzNextNotice(buffer);
        console.log('========onStartAnswerTzNextNotice=============', data);
        director.emit(EventMsgKes.msgStartAnswerTzNextNotice, data);
    }

    // 每日答题--结算通知
    private onStartAnswerTzEndNotice(buffer: Uint8Array): void {
        let data = decodeStartAnswerTzEndNotice(buffer);
        console.log('========onStartAnswerTzEndNotice=============', data);
        director.emit(EventMsgKes.msgStartAnswerTzEndNotice, data);
    }

    //每日答题--结算领取奖励
    private onStartAnswerTzEndGetRewardRes(buffer: Uint8Array): void {
        let data = decodeStartAnswerTzEndGetRewardRes(buffer);
        console.log('========onStartAnswerTzEndGetRewardRes=============', data);
        director.emit(EventMsgKes.msgStartAnswerTzEndGetRewardRes, data);
    }

    //每日挑战--复活
    private onStartAnswerTzFuHuoRes(buffer: Uint8Array): void {
        let data = decodeStartAnswerTzFuHuoRes(buffer);
        console.log('========onStartAnswerTzFuHuoRes=============', data);
        director.emit(EventMsgKes.msgStartAnswerTzFuHuoRes, data);
    }
    //====================================每日挑战 end===============================================
    //主题赛--- 场景
    private onThemeMatchGameRes(buffer:Uint8Array): void{
        let data = decodeThemeMatchGameRes(buffer);
        console.log('========onThemeMatchGameRes=============', data);
        director.emit(EventMsgKes.msgThemeMatchGameRes, data);
    }
}


